// pages/supportme/supportme.js
var tid = null;
const app = getApp()
const forumsPageSize = 5;
var pageIndex = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    forums: null, // 帖子
    
    supportData: null,
    
    image: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
  },

  topicClick: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.id + '&acid=' + getApp().globalData.acid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    tid = options.tid;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;

    // 活动
    wx.request({
      url: getApp().globalData.current_url,
      success(res) {
        console.log(res);
        if (res.data.data.current == null || res.data.data.current == undefined) {
        } else {
          var leftTime = res.data.data.current.end_time - res.data.data.current.current;
          that.countDown(leftTime);
        }
      }
    })


    console.log(tid, getApp().globalData.userInfo.userId, getApp().globalData.acid)
    // 获取支持我的数据
    wx.request({
      url: getApp().globalData.support_get_url,
      data: {
        tid: tid,
        userId: getApp().globalData.userInfo.userId,
        acid: getApp().globalData.acid,
      },
      success: function(res) {
        console.log(res);

        that.setData({
          supportData: res.data.data,
        });
      },
    })

    // 帖子
    pageIndex = 0;
    this.requestForums();
  },


  // 倒计时方法
  countDown: function (leftTime) {
    var that = this;

    var leftHours = Math.floor(leftTime / 60 / 60);
    if (leftHours <= 9) {
      leftHours = '0' + leftHours;
    }
    var leftMinutes = Math.floor(leftTime / 60 % 60);
    if (leftMinutes <= 9) {
      leftMinutes = '0' + leftMinutes;
    }
    var leftSeconds = Math.floor(leftTime % 60);
    if (leftSeconds <= 9) {
      leftSeconds = '0' + leftSeconds;
    }
    that.setData({
      activityTime: leftHours + ":" + leftMinutes + ":" + leftSeconds,
    })

    if (leftTime > 0) {
      setTimeout(function () {
        leftTime--;
        that.countDown(leftTime);
      }, 1000);
    }
  },



  // 请求帖子信息
  requestForums: function () {
    var that = this;

    wx.request({
      url: getApp().globalData.getHomeForum_url,
      data: {
        pageSize: forumsPageSize,
        pageIndex: pageIndex
      },
      success(res) {
        console.log(res)
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
          return
        }
        var tempArr = res.data.data.forumList;
        if (tempArr != null && tempArr.length > 0) {
          if (pageIndex == 0) {
            that.setData({
              forums: tempArr
            })
          } else {
            var forumsList = that.data.forums;
            that.setData({
              forums: forumsList.concat(tempArr)
            })
          }
          pageIndex++;
        }
      },
      complete() {
        wx.stopPullDownRefresh();
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    pageIndex = 0;
    this.requestForums();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestForums();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    console.log(e)
    if (e.from == 'menu') { // 分享小程序

      return {
        title: '快来加入闲置会发光，做最环保的时髦精>>',
        path: '/pages/login/login',
        imageUrl: '/resources/share_miniprogram.png',
      }
    }
    return {
      title: '我正在32个女生衣柜里找新欢，就差你支持了>>',
      path: '/pages/login/login?shareUserId=' + getApp().globalData.userInfo.userId + '&acid=' + getApp().globalData.acid + '&tid=' + tid + '&nickname=' + getApp().globalData.userInfo.nickName,
      imageUrl: '/resources/share_supportme.png',
    }

  }
})
